import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import { Logger,AppConfig,LocalInfo,RouterMap } from 'common'
import data_preferences from '@ohos.data.preferences';
AppStorage.SetOrCreate('token', "");

let localStorage = new LocalStorage({ user_avatar: null,userInfo:null});
export default class EntryAbility extends UIAbility {
  storage: LocalStorage = localStorage

  onCreate(want, launchParam) {
    Logger.info("Ability onCreate")
    LocalInfo.getInstance().setAppContext(this.context)
    RouterMap.init()


    let preferences = data_preferences.getPreferences(this.context,"gsy_github_harmony")
    preferences.then((res)=>{
      res.get("github_token","").then((token)=>{
        if(token){
          AppStorage.Set('token',token)
        }
      }).catch((err)=>{
        Logger.info("github_token error")
      })
      res.get("user_name","").then((userName)=>{
        if(userName){
          AppStorage.Set('userName',userName)
        }
      })
    })
  }

  onDestroy() {
    Logger.info("Ability onDestroy")
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    Logger.info("Ability onWindowStageCreate")
    let windowClass = null;
    windowStage.getMainWindow().then((value)=>{
      windowClass = value;
      globalThis.screenHeight = value.getWindowProperties().windowRect.height
    })

    windowStage.loadContent('pages/SplashPage',this.storage,(err, data) => {
      if (err.code) {
        Logger.error('Failed to load the content. Cause: %{public}s', JSON.stringify(err))
        return;
      }
      Logger.info('Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '')
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    Logger.info("Ability onWindowStageDestroy")
  }

  onForeground() {
    // Ability has brought to foreground
    Logger.info("Ability onForeground")
  }

  onBackground() {
    // Ability has back to background
    Logger.info("Ability onBackground")
  }
}
